import _ from 'lodash';
import decimal from 'decimal.js';
import { carTypes } from './enums';

export const toPoint = (percent) => {
	const flt = percent.split('%');
	if (flt.length === 1) {
		return flt;
	}
	return flt[0];
}

export const toPointB = (percent) => {
	const flt = percent.split('%');
	if (flt.length === 1) {
		return flt;
	}
	return flt[0];
}

export const toPercent = (val) => {
	if (typeof val === 'object') {
		const values = val.map(item => {
			const value = calc.mul(item, 100);
			return `${toRound(value)}%`;
		})
		return values;
	} 
	
	const value = calc.mul(val, 100);
	return `${toRound(value)}%`;
}

export const fixFloat = (float, fix) => {
	if (!float) {
		return float;
	}
	return +parseFloat(float).toPrecision(fix || 9);
}

export const toRound = (float) => {
	return _.round(float, 2);
}

export const calc = {
	mul: (...vals) => {
		let ret = new decimal(vals[0] || 0);
		// 移除第一个元素
		vals.shift();
		vals.map(item => {
			ret = ret.mul(item || 0);
		});
		return ret.toNumber();
		// return (decimal.mul(...vals)).toNumber();
	},
	plus: (...vals) => {
		return (decimal.add(...vals)).toNumber();
	},
	div: (...vals) => {
		return (decimal.div(...vals)).toNumber();
	},
	minus: (a, b) => {
		return (new decimal(a).minus(b)).toNumber()
	}
}


export const getStoreParams = (that) => {
	const eventChannel = that.getOpenerEventChannel();
	const params = eventChannel?.emitCache?.params?.[0]?.[0];
	return params;
}

export const changeCheckbox = (that, oldVal, newVal, field, getItemDefault) => {
	let fields = field;
	if (typeof field === 'string') {
		fields = [field];
	}
	// 判断删除
	const diff = _.difference(oldVal, newVal);
	if (diff[0]) {
		if (newVal.length === 0) {
			that.formData[fld] = [];
		} else {
			const idx = oldVal.indexOf(diff[0]);
			fields.map(fld => {
				const values = that.formData[fld];
				values.splice(idx, 1)
				that.formData[fld] = values;
			})
		}
	}

	// 判断新增
	const diffAdd = _.difference(newVal, oldVal);
	if (diffAdd[0] && newVal.length - (oldVal || []).length === 1) {
		const idx = newVal.indexOf(diffAdd[0]);
		fields.map(fld => {
			let values = that.formData[fld];
			if (newVal.length === 1) {
				values = values[0] ? [values[0]] : [getItemDefault(diffAdd[0], fld)];
			} else {
				values.splice(idx, 0, getItemDefault(diffAdd[0], fld));
			}
			that.formData[fld] = values;
		})
	}
}

export const getCef = (that,item) => {
	const obj = _.find(that.$t('cefParts'), {
		name: item,
	});
	if (!obj) {
		console.log('未找到材料的排放因子');
		return '';
	}
	return obj.value;
}

export const getCarTypeName = (carType) => {
	const {
		text,
	} = _.find(carTypes, {
		carType: carType.toString(),
	}) || {};
	return text;
}

export const setTabbarItems = (that) => {
	const current = getCurrentPages();
	const curr = current.pop().route;
	uni.setNavigationBarTitle({
		title: that.$t(`paths.${curr}`)
	});
	
	uni.setTabBarItem({
		index: 0,
		text: that.$t('about.index'),
	});
	uni.setTabBarItem({
		index: 1,
		text: that.$t('about.my'),
	});
}